package priv.happy.mealpickpro.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import priv.happy.mealpickpro.domain.LoginLog;
import priv.happy.mealpickpro.service.LoginLogService;
import priv.happy.mealpickpro.vo.ApiResponse;

/**
 * @Author: HAPPY
 * @Project_name: mealPick
 * @Package_name: priv.happy.mealpick.controller
 * @Date: 2023/10/11 9:33
 * @Description:
 */
@RestController
@RequestMapping("/api/v2/loginLog")

public class LoginLogController {
    @Autowired
    LoginLogService loginLogService;

    @GetMapping()
    public ApiResponse<Page<LoginLog>> getLoginLogByUserId(String userId, Integer current, Integer size) {
        ApiResponse<Page<LoginLog>> apiResponse = new ApiResponse<>();
        Page<LoginLog> loginLogPage = new Page<>(current, size);

        QueryWrapper<LoginLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.orderBy(true, false, "login_time");
        loginLogPage = loginLogService.page(loginLogPage, queryWrapper);

        if (loginLogPage != null && (loginLogPage.getTotal() >= 0)) {
            return apiResponse.setData(loginLogPage).setSuccess(true).setCode(200).setMessage("查询登录日志成功");
        }
        return apiResponse.setSuccess(false).setCode(500).setMessage("查询登录日志失败");
    }
}
